package com.yunrui.device.controller;

import com.yunrui.device.entity.CabinetType;
import com.yunrui.device.service.CabinetTypeService;
import com.yunrui.device.common.Result;
import com.yunrui.device.dto.CabinetTypeDTO;
import org.springframework.beans.BeanUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "柜机类型管理接口")
@RestController
@RequestMapping("/cabinet/type")
public class CabinetTypeController {

    @Autowired
    private CabinetTypeService cabinetTypeService;

    // 查询所有
    @ApiOperation("获取所有柜机类型")
    @GetMapping("/list")
    public Result<List<CabinetType>> list() {
        List<CabinetType> list = cabinetTypeService.list();
        return Result.success(list);
    }

    // 新增
    @ApiOperation("添加柜机类型")
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody CabinetTypeDTO dto) {
        CabinetType type = new CabinetType();
        BeanUtils.copyProperties(dto, type);
        boolean success = cabinetTypeService.save(type);
        return success ? Result.success(true) : Result.error("添加失败");
    }

    // 修改
    @ApiOperation("更新柜机类型")
    @PutMapping("/update")
    public Result<Boolean> update(@RequestBody CabinetTypeDTO dto) {
        CabinetType type = new CabinetType();
        BeanUtils.copyProperties(dto, type);
        boolean success = cabinetTypeService.updateById(type);
        return success ? Result.success(true) : Result.error("更新失败");
    }

    // 删除
    @ApiOperation("删除柜机类型")
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> delete(@ApiParam("类型ID") @PathVariable Long id) {
        boolean success = cabinetTypeService.removeById(id);
        return success ? Result.success(true) : Result.error("删除失败");
    }
}